package com.guigu.buy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.buy.pojo.Customer;
import com.guigu.buy.pojo.Merchant;
import com.guigu.buy.pojo.Supplier;
import com.guigu.buy.service.ICustomerService;
import com.guigu.buy.service.ISupplierService;
import com.guigu.buy.utils.PageUtils;
import com.guigu.buy.vo.SupplierVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/buy/supplier")
public class SupplierController {
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private ICustomerService customerService;
    /**
     * 供应商入驻
     * @param supplier
     * @return
     */
    @ResponseBody
    @PostMapping("/insertSupplier")
    public int insertSupplier(@RequestBody Supplier supplier){
        System.out.println(supplier);
        int i = supplierService.insertSupplier(supplier);
        System.out.println(i);
        return i;
    }

    /**
     * 分页查询供应商
     * @param pageNo
     * @param pageSize
     * @param vo
     * @return
     */
    @RequestMapping("/list/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<Supplier> list(@PathVariable int pageNo , @PathVariable int pageSize, @RequestBody SupplierVo vo){
        PageUtils<Supplier> pageUtils = supplierService.AllList(pageNo, pageSize, vo);
        return pageUtils;
    }

    @RequestMapping("/selById")
    @ResponseBody
    public  Supplier selById(int id){
        QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Supplier one = this.supplierService.getOne(wrapper);
        return one;
    }

    @RequestMapping("/selAudit/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<Supplier> selAudit(@PathVariable int pageNo , @PathVariable int pageSize, @RequestBody SupplierVo vo){
        PageUtils<Supplier> supplierPageUtils = this.supplierService.selAudit(pageNo, pageSize, vo);
        return supplierPageUtils;
    }

    /**
     * 修改供应商审核状态
     * @param supplier
     * @return
     */
    @RequestMapping("/updateOk")
    @ResponseBody
    public boolean updateOk(@RequestBody Supplier supplier){

        supplier.setCheckTime(new Date());
        supplier.setUserSupplierTag(1);
        supplier.setSupplierStatus(0);
        boolean b = this.supplierService.updateById(supplier);
        if(supplier.getUserSupplierTag().equals(0)){
            QueryWrapper<Customer> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",supplier.getId());
            Customer one = this.customerService.getOne(wrapper);
            one.setUserSupplier(1);
            this.customerService.updateById(one);
        }
        return b;
    }

    /**
     * 修改未通过
     * @param supplier
     * @return
     */
    @RequestMapping("/updateNot")
    @ResponseBody
    public boolean updateNot(@RequestBody Supplier supplier){
        supplier.setUserSupplierTag(3);
        boolean b = this.supplierService.updateById(supplier);
        return b;
    }
}
